<?php
//vi tri trang hien thi
$page = 1;


//so luong record hien thi
$offset = 20;

//tong so record
$totals = 0;


//tong so trang
//hien thi  so  trang  tren control
$show_page_num = 10;

//dua vao trang hien tai se co so nho va lon nhat trang  trong control
$page_start = 0;
$page_end = 0;

//lay vi tri trang tu request
if (!empty($_GET["page"])) {
    $page = $_GET["page"];
}
if (!empty($_GET["offset"])) {
    $offset = $_GET["offset"];
}

//hien thi control
function show_pager_control($totals, $offset, $page,$show_page_num ,$page_start, $page_end,$query) {

    $page_count = $totals / $offset;
    if (($totals % $offset) > 0) {
        $page_count++;
    }
    if ($page <= ($page_count - ($show_page_num / 2))) {
        if ($page >= $show_page_num / 2) {
            $page_start = $page - $show_page_num / 2;
            if ($page_start <= 0) {
                $page_start = 1;
            }
            $page_end = $page_start + $show_page_num - 1;
        } else {
            $page_start = 1;
            $page_end = $page_start + $show_page_num - 1;
            if ($page_end > $page_count) {
                $page_end = $page_count;
            }
        }
    } else {
        $page_start = $page_count - $show_page_num;
        if ($page_start <= 0) {
            $page_start = 1;
        }
        $page_end = $page_count;
    }



    if ($page_count > 1) {
?>
        <table>
            <tr>

                <td><a href="?page=<?= ($page > 1) ? ($page - 1) : $page; ?><?=$query;?>" >Trang trước</a></td>
                <td width="10"></td>
        <?
        for ($i = $page_start; $i <= $page_end; $i++) {
        ?>
                <td width="25" align="center"><a href="?page=<?= $i; ?><?=$query;?>" ><?= $i; ?></a> </td>
        <? } ?>
        <td width="10"></td>
        <td><a href="?page=<?= ($page < $page_count) ? ($page + 1) : $page; ?><?=$query;?>" >Trang sau</a></td>
    </tr>
</table>
<?
    }
}
?>

